function [SampledTrueLabel, SampledData, SampledOnlyTrueLabel, UnSampledData, DataSampler, SampledDataSize, SampledLabelSize] = Ddavid_sample_label(AllData, TrueLabel, SampledDataPercent, SampledLabelPercent)

% [SampledTrueLabel, SampledData, SampledOnlyTrueLabel, UnSampledData, DataSampler, SampledDataSize, SampledLabelSize] = Ddavid_sample_label(AllData, TrueLabel, SampledDataPercent, SampledLabelPercent)
%
% <Input>
% AllData: [n*m], n is the size of dataset, m is the number of features
% TrueLabel: [n*k], n is the size of dataset, k is the number of labels
% SampledDataPercent: double, the percent of the incomplete labeled data
% SampledLabelPercent: double, the percent of the max label number of
%                      imcomplete labeled data
%
% <Output>
% SampledTrueLabel: [n*k]
% SampledData: [SampledDataSize * m]
% SampledOnlyTrueLabel: [SampledDataSize * k]
% UnSampledData: [(n - SampledDataSize) * m]
% DataSampler: [1*n], the value is {0, 1} means the data is not sampled or
%              is sampled
% SampledDataSize: int, the size of the incomplete labeled data
% SampledLabelSize: int, the size of the max label number of imcomplete
%                   labeled data

AllDataSize = size(TrueLabel, 1);
LabelSize = size(TrueLabel, 2);
LabelSizePerInstance = sum(sum(TrueLabel == 1, 1), 2) / AllDataSize;

SampledDataSize = floor(AllDataSize * SampledDataPercent / 100.0);
SampledLabelSize = floor(LabelSizePerInstance * SampledLabelPercent / 100.0);
if(SampledLabelSize == LabelSize)
    SampledLabelSize = SampledLabelSize - 1;
end
if(SampledLabelSize < 1)
    SampledLabelSize = 1;
end

disp(['SampledDataSize = ' int2str(SampledDataSize)]);
disp(['LabelSizePerInstance = ' int2str(LabelSizePerInstance)]);
disp(['SampledLabelSize = ' int2str(SampledLabelSize)]);

SampledTrueLabel = ones(AllDataSize, LabelSize) * (-1);

DataSampler = [ones(1, SampledDataSize) zeros(1, AllDataSize - SampledDataSize)];

for Index = AllDataSize:-1:2
    NewIndex = randi(Index);
    Temp = DataSampler(NewIndex);
    DataSampler(NewIndex) = DataSampler(Index);
    DataSampler(Index) = Temp;
end

for Index = 1:AllDataSize
    if(DataSampler(Index) ~= 0)
        LabelList = find(TrueLabel(Index, :) ~= -1);
        TempLabelSize = size(LabelList, 2);
        if(TempLabelSize > SampledLabelSize)
            for LabelIndex = TempLabelSize:-1:2
                NewLabelIndex = randi(LabelIndex);
                Temp = LabelList(NewLabelIndex);
                LabelList(NewLabelIndex) = LabelList(LabelIndex);
                LabelList(LabelIndex) = Temp;
            end
            SampledTrueLabel(Index, LabelList(1, 1:SampledLabelSize)) = 1;
        else
            SampledTrueLabel(Index, LabelList) = 1;
        end
    end
end

SampledData = AllData(DataSampler == 1, :);
SampledOnlyTrueLabel = SampledTrueLabel(DataSampler == 1, :);
UnSampledData = AllData(DataSampler == 0, :);
